/* Copyright */
/*******************************************************************************
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Tsunagi-BPM
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *******************************************************************************/
 /* Copyright End */

package tsunagi.common.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import tsunagi.common.Lang;
import tsunagi.common.SystemException;

public enum StringUtil {
	INSTANCE;
	
	
	public boolean isFirstWord(String word, String val) {
		val = val.trim();
		return val.indexOf(word) == 0 ? true: false;
	}
	
	public boolean isFirstChar(String oneChar, String val) {
		val = val.trim();
		return val.indexOf(oneChar) == 0 ? true: false;
	}
	
	public boolean isCharIncluded(String oneChar, String val) {
		val = val.trim();
		return val.indexOf(oneChar) > -1 ? true: false;
	}

	public boolean isBlank(String val) {
		return val==null || val.equals(Lang.STRING_EMPTY) ? true: false;
	}
	
	public List<String> getTokens(String val) {
		ArrayList<String> result = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(val);
		while (st.hasMoreTokens()) {
			result.add(st.nextToken());
		}
		return result;
	}
	
	public List<String> removeDuplicate(List<String> data) {
		List<String> result = new ArrayList<String>(data.size());
		for(String val: data) {
			if (!exists(val, result)) {
				result.add(val);
			}
		}
		return result;
	}
	
	private boolean exists(String val, List<String> data) {
		boolean result = false;
		for(String s: data) {
			if (s.equals(val)) {
				result = true;
				break;
			}
		}
		return result;
	}
	
	public List<String> getTokens(String val, String delm) {
		ArrayList<String> result = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(val, delm);
		while (st.hasMoreTokens()) {
			result.add(st.nextToken());
		}
		return result;
	}
	
	public String getContent(InputStream is) {
		StringBuffer result = new StringBuffer();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		try {
			while(true) {
				String line = br.readLine();
				if (line == null) {
					break;
				} else {
					line += Lang.NEW_LINE;
				}
				result.append(line);
			}
			br.close();
		}catch(Exception e) {
			throw new SystemException(e.getMessage(), e);
		}
		return result.toString();
	}
	
	public String listToString(List<String> list, String separator) {
		StringBuffer result = new StringBuffer();
		if (list == null || list.size() == 0) {
			return result.toString();
		}
		
		for(String id: list) {
			result.append(id);
			result.append(separator);
		}
		result.deleteCharAt(result.length()-1); // remove the last separator
		return result.toString();
	}

	public static void main(String[] args) {
		List<String> sts = StringUtil.INSTANCE.getTokens("name  This is a pen.", "=");
		//List<String> sts = StringUtil.INSTANCE.getTokens("This is a pen.");
		for(String st: sts)
			System.out.println("[" + st + "]");
	}
}

